﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WaterRevenueMS.WebUI.Common;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Service.BasicSet.Impl;
using WaterRevenueMS.WebUI.Extension.Filters;
using WaterRevenueMS.Domain.Common.Models;
using WaterRevenueMS.Core.Service.BasicSet;
using WaterRevenueMS.Domain.ViewModels.BasicSet.BankSet;
using WaterRevenueMS.Domain.ViewModels.BasicSet.CompanySet;
using WaterRevenueMS.Componnent.Entities.BasicSet;
using System.Linq.Expressions;
using WaterRevenueMS.WebUI.Extension.MvcHelper;
using WaterRevenueMS.Componnent.Common;

namespace WaterRevenueMS.WebUI.Controllers
{
    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class CompanySetController : AdminController
    {
        #region 属性
        [Import]
        public IBankSetService BankSetService { get; set; }

        [Import]
        public ICompanySetService CompanySetService { get; set; }
        #endregion

        [AdminLayoutAttribute]
        public ActionResult Index()
        {
            var model = new CompanySetModel();
            #region 银行设置
            var parentModuleData = BankSetService.BankSets.Where(t => t.IsDeleted == false)
            .Select(t => new CompanySetModel
            {
                Id = t.Id,
                BankSetName = t.Yin_HMC
            });
            foreach (var item in parentModuleData)
            {
                model.Search.BankSetItems.Add(new SelectListItem { Text = item.BankSetName, Value = item.Id.ToString() });
            }
            #endregion
            return View(model);
        }

        [AdminPermission(PermissionCustomMode.Ignore)]
        public ActionResult List(DataTableParameter param)
        {
            int total = CompanySetService.CompanySets.Count(t => t.IsDeleted == false);

            var expr = BuildSearchCriteria();

            var filterResult = CompanySetService.CompanySets.Where(expr).Select(t => new CompanySetModel
            {
                Id = t.Id,
                BankSetName = t.BankSet != null ? t.BankSet.Yin_HMC : "",
                Gong_SBZ = t.Gong_SBZ,
               
                Dan_WMC = t.Dan_WMC,
                Dan_WJC = t.Dan_WJC,
                Dan_WMCE = t.Dan_WMCE,
                Dan_WDZ = t.Dan_WDZ,
                Dian_H = t.Dian_H,
                Chuan_Z = t.Chuan_Z,
                Fu_ZR = t.Fu_ZR,
                Lian_XR = t.Lian_XR,
                You_ZBM = t.You_ZBM,
                Qi_YZY = t.Qi_YZY,
                Qi_YYX = t.Qi_YYX,
                Yin_HZH = t.Yin_HZH,
                Shui_H = t.Shui_H,
                OrderNo = t.OrderNo

            }).OrderBy(t => t.Id).Skip(param.iDisplayStart).Take(param.iDisplayLength).ToList();

            int sortId = param.iDisplayStart + 1;

            var result = from c in filterResult
                         select new[]
                        {
                            sortId++.ToString(), 
                            c.BankSetName,
                            c.Dan_WMC,
                            c.Dan_WJC,
                            c.Dan_WMCE,
                            c.Dan_WDZ,
                            c.Dian_H,
                            c.Chuan_Z,
                            c.Fu_ZR,
                            c.Lian_XR,
                            c.You_ZBM,
                            c.Qi_YZY,
                            c.Qi_YYX,
                            c.Yin_HZH,
                            c.Shui_H,
                            c.OrderNo.ToString(),
                            c.Id.ToString()
                        };

            return Json(new
            {
                sEcho = param.sEcho,
                iDisplayStart = param.iDisplayStart,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = result
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Create()
        {
            var model = new CompanySetModel();
            InitBankSetModel(model);
            return PartialView(model);
        }

        [HttpPost]
        [AdminOperateLog]
        public ActionResult Create(CompanySetModel model)
        {
            if (ModelState.IsValid)
            {

                OperationResult result = CompanySetService.Insert(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }
            }
            else
            {
                return PartialView(model);
            }
        }

        public ActionResult Edit(int Id)
        {
            var model = new CompanySetModel();
            var entity = CompanySetService.CompanySets.FirstOrDefault(t => t.Id == Id);
            if (null != entity)
            {
                model = new CompanySetModel
                {
                    Id = entity.Id,
                    Yin_HSZ_No = entity.Yin_HSZ_No,
                    Gong_SBZ = entity.Gong_SBZ,
                    Dan_WMC = entity.Dan_WMC,
                    Dan_WJC = entity.Dan_WJC,
                    Chuan_Z = entity.Chuan_Z,
                    Dian_H = entity.Dian_H,
                    Lian_XR = entity.Lian_XR,
                    Dan_WMCE = entity.Dan_WMCE,

                    Dan_WDZ = entity.Dan_WDZ,
                    Fu_ZR = entity.Fu_ZR,
                    You_ZBM = entity.You_ZBM,
                    Qi_YZY = entity.Qi_YZY,
                    Qi_YYX = entity.Qi_YYX,
                    Yin_HZH = entity.Yin_HZH,
                    Shui_H = entity.Shui_H,


                };
                InitBankSetModel(model);
            }
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Edit(CompanySetModel model)
        {
            if (ModelState.IsValid)
            {
                OperationResult result = CompanySetService.Update(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }

            }
            else
            {
                return PartialView(model);
            }
        }


        public ActionResult Delete(int Id)
        {
            OperationResult result = CompanySetService.Delete(Id);
            return Json(result);
        }


        private Expression<Func<CompanySet, Boolean>> BuildSearchCriteria()
        {
            DynamicLambda<CompanySet> bulider = new DynamicLambda<CompanySet>();
            Expression<Func<CompanySet, Boolean>> expr = null;
            if (!string.IsNullOrEmpty(Request["Gong_SBZ"]))
            {
                var data = Request["Gong_SBZ"].Trim();
                Expression<Func<CompanySet, Boolean>> tmp = t => t.Gong_SBZ.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Dan_WMC"]) )
            {
                var data = Request["Dan_WMC"].Trim();
                Expression<Func<CompanySet, Boolean>> tmp = t => t.Dan_WMC.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Dan_WJC"]))
            {
                var data = Request["Dan_WJC"].Trim();
                Expression<Func<CompanySet, Boolean>> tmp = t => t.Dan_WJC.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Dan_WMCE"]))
            {
                var data = Request["Dan_WMCE"].Trim();
                Expression<Func<CompanySet, Boolean>> tmp = t => t.Dan_WMCE.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Dan_WDZ"]))
            {
                var data = Request["Dan_WDZ"].Trim();
                Expression<Func<CompanySet, Boolean>> tmp = t => t.Dan_WDZ.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Dian_H"]))
            {
                var data = Request["Dian_H"].Trim();
                Expression<Func<CompanySet, Boolean>> tmp = t => t.Dian_H.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Chuan_Z"]))
            {
                var data = Request["Chuan_Z"].Trim();
                Expression<Func<CompanySet, Boolean>> tmp = t => t.Chuan_Z.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Fu_ZR"]))
            {
                var data = Request["Fu_ZR"].Trim();
                Expression<Func<CompanySet, Boolean>> tmp = t => t.Fu_ZR.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Lian_XR"]))
            {
                var data = Request["Lian_XR"].Trim();
                Expression<Func<CompanySet, Boolean>> tmp = t => t.Lian_XR.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["You_ZBM"]))
            {
                var data = Request["You_ZBM"].Trim();
                Expression<Func<CompanySet, Boolean>> tmp = t => t.You_ZBM.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Qi_YZY"]))
            {
                var data = Request["Qi_YZY"].Trim();
                Expression<Func<CompanySet, Boolean>> tmp = t => t.Qi_YZY.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Qi_YYX"]))
            {
                var data = Request["Qi_YYX"].Trim();
                Expression<Func<CompanySet, Boolean>> tmp = t => t.Qi_YYX.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Yin_HSZ_No"]) && Request["Yin_HSZ_No"] != "0")
            {
                var data = Convert.ToInt32(Request["Yin_HSZ_No"]);
                Expression<Func<CompanySet, Boolean>> tmp = t => (t.Id == data) || (t.Yin_HSZ_No == data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Yin_HZH"]))
            {
                var data = Request["Yin_HZH"].Trim();
                Expression<Func<CompanySet, Boolean>> tmp = t => t.Yin_HZH.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Shui_H"]))
            {
                var data = Request["Shui_H"].Trim();
                Expression<Func<CompanySet, Boolean>> tmp = t => t.Shui_H.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            Expression<Func<CompanySet, Boolean>> tmpSolid = t => t.IsDeleted == false;
            expr = bulider.BuildQueryAnd(expr, tmpSolid);

            return expr;
        }

        private void InitBankSetModel(CompanySetModel model)
        {
            var parentModuleData = BankSetService.BankSets.Where(t => t.IsDeleted == false)
                        .Select(t => new CompanySetModel
                        {
                            Id = t.Id,
                            BankSetName = t.Yin_HMC
                        });
            foreach (var item in parentModuleData)
            {
                model.BankSetItems.Add(new SelectListItem { Text = item.BankSetName, Value = item.Id.ToString() });
            }
        }
    }
}
